package com.zyguo.sdk.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class BLThreadPool {
	private int corePoolSize = 3;
	private int maxPoolSize = 30;
	private ThreadPoolExecutor threadPool = null;

	public int getCorePoolSize() {
		return corePoolSize;
	}

	public void setCorePoolSize(int corePoolSize) {
		this.corePoolSize = corePoolSize;
	}

	public int getMaxPoolSize() {
		return maxPoolSize;
	}

	public void setMaxPoolSize(int maxPoolSize) {
		this.maxPoolSize = maxPoolSize;
	}
 
	public BLThreadPool() {
		// 构造一个线程池
		threadPool = new ThreadPoolExecutor(corePoolSize, maxPoolSize, 0L,
				TimeUnit.MICROSECONDS, new ArrayBlockingQueue<Runnable>(
						100000), new ThreadPoolExecutor.DiscardPolicy());
		
	}
	
	public void execute(Runnable r) {
		threadPool.execute(r);
	}

	public <T> Future<T> submit(Callable<T> task ){
		return threadPool.submit( task );
	}
	
	public Future<?> submit(Runnable task ){
		return threadPool.submit( task );
	}

	public <T> Future<T> submit(Runnable task, T result ){
		return threadPool.submit( task, result );
	}
	
	// 调用举例
	public static void main(String[] args) {
		BLThreadPool threadPool = new BLThreadPool();

		for (int i = 1; i <= 3; i++) {
			try {
				String task = "task@ " + i;
				System.out.println("创建任务并提交到线程池中：" + task);
				threadPool.execute( new Runnable() {
					public void run() {
						
					}
				});

				Thread.sleep(1);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
